//
//  RoomInformationViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/4/12.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "RoomInformationViewController.h"
#import "BottomActionView.h"
#import "EditorTableViewCell.h"
#import "RoomNameViewController.h"
#import "MachineSelectViewController.h"
#import <MBProgressHUD.h>
#import "ProjectData.h"
#import "IndoorEquipDataResp.h"
#import "MachineSelectTableViewCell.h"
#import "GuidData.h"
#import "IndoorEquipData.h"
#import "SNJMachineSelectViewController.h"
#import "ProjectManager.h"
#import "LoadFactorDataResp.h"
#import "UITapView.h"
#import "SelectXSViewController.h"
#import "FjpgViewController.h"
#import "TerminalTypeTableViewCell.h"
#import "Project_Step3ViewController.h"
#import "IQKeyboardManager.h"

#define RoomInformationCell @"RoomInformationCell"


@interface RoomInformationViewController () {
    BOOL isEditing;
    BOOL isNexting;
}

@property (nonatomic,strong) RoomData *roomData;
@property (nonatomic,strong) UIBarButtonItem *rightBtn;

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) BottomActionView *footerView;
@property (nonatomic,strong) EditorTableViewCell *areaCell;
@property (nonatomic,strong) UITextField *hideField;

@property (nonatomic,copy) NSString *roomName;
@property (nonatomic,assign) double roomArea;
@property (nonatomic,copy) NSString *roomTerminalType;
@property (nonatomic,assign) double roomColdFactor;
@property (nonatomic,assign) double roomHeatFactor;
@property (nonatomic,assign) double roomColdLoad;
@property (nonatomic,assign) double roomHeatLoad;

@end

@implementation RoomInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initData];
    [self addObserver];
    [self setupViews];
}

/**
 *  初始化数据
 */
- (void)initData {
    if (self.currentIndex >= kProject.rooms.count) {
        return;
    }
    if (self.currentIndex == kProject.rooms.count - 1) {
        self.navigationItem.rightBarButtonItem = nil;
    }
    self.roomData = [kProject.rooms objectAtIndex:self.currentIndex];
    [self copyRoomData];
}

- (void)copyRoomData {
    self.roomName = self.roomData.room_name;
    self.roomArea = self.roomData.area;
    self.roomTerminalType = self.roomData.terminal_type;
    self.roomColdFactor = self.roomData.cold_factor;
    self.roomHeatFactor = self.roomData.heat_factor;
    self.roomHeatLoad = self.roomData.heat_load;
    self.roomColdLoad = self.roomData.cold_load;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
//    [IQKeyboardManager sharedManager].enable = NO;
}

-(void)viewWillDisappear:(BOOL)animated{
    [self.hideField resignFirstResponder];
//    [IQKeyboardManager sharedManager].enable = YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 初始化视图
- (void)setupViews{
    self.title = @"房间编辑";

    self.hideField = [[UITextField alloc]initWithFrame:CGRectMake(-100, -100, 1, 1)];
    [self.view addSubview:self.hideField];
    
    //自定义返回按钮
    UIBarButtonItem *returnBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(onReturn)];
    self.navigationItem.leftBarButtonItem = returnBtn;
    self.rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStyleDone target:self action:@selector(saveAndNext)];
    self.navigationItem.rightBarButtonItem = self.rightBtn;
    if (self.currentIndex == kProject.rooms.count - 1) {
        self.rightBtn.title = @"完成";
    }
    self.tableView = [[UITableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStyleGrouped];
    [self.tableView registerClass:[EditorTableViewCell class] forCellReuseIdentifier:RoomInformationCell];
    [self.tableView registerClass:[TerminalTypeTableViewCell class] forCellReuseIdentifier:@"ter"];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(100 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
        if (self.areaCell != nil) {
            [self.areaCell.editorView becomeFirstResponder];
        }
    });
}

#pragma mark - 事件响应
- (void)onSave {
    //[self.navigationController popViewControllerAnimated:YES];
}

- (void)onReturn {
    if (self.roomInfoChangedBlock != nil) {
        self.roomInfoChangedBlock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)saveAndNext {
    isNexting = YES;
    if (isEditing) {
        if (self.roomArea == 0) {
            [self onNext];
        }
        [self.hideField becomeFirstResponder];
    } else {
        [self onNext];
    }
}


- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginediting:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endediting:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)beginediting:(id)object {
    isEditing = YES;
}

- (void)endediting:(id)object {
    isEditing = NO;
}



- (void)onNext {
    if (self.roomInfoChangedBlock != nil) {
        self.roomInfoChangedBlock();
    }
    self.currentIndex++;
    if (self.currentIndex == kProject.rooms.count - 1) {
        self.rightBtn.title = @"完成";
    }
    else if (self.currentIndex > kProject.rooms.count - 1) {
        //完成下一步
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    self.roomData = [kProject.rooms objectAtIndex:self.currentIndex];
    [self copyRoomData];
    [self.tableView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(50 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
        if (self.areaCell != nil) {
            [self.areaCell.editorView becomeFirstResponder];
        }
    });
    isNexting = NO;
}

- (BOOL)needReloadTable {
    self.roomName = self.roomData.room_name;
    self.roomArea = self.roomData.area;
    self.roomTerminalType = self.roomData.terminal_type;
    self.roomColdFactor = self.roomData.cold_factor;
    self.roomHeatFactor = self.roomData.heat_factor;
    self.roomHeatLoad = self.roomData.heat_load;
    self.roomColdLoad = self.roomData.cold_load;
    if (![self.roomName isEqualToString:self.roomData.room_name]) {
        return YES;
    }
    if (![self.roomTerminalType isEqualToString:self.roomData.terminal_type]) {
        return YES;
    }
    if (self.roomArea != self.roomData.area) {
        return YES;
    }
    if (self.roomColdFactor != self.roomData.cold_factor) {
        return YES;
    }
    if (self.roomHeatFactor != self.roomData.heat_factor) {
        return YES;
    }
    if (self.roomHeatLoad != self.roomData.heat_load) {
        return YES;
    }
    if (self.roomColdLoad != self.roomData.cold_load) {
        return YES;
    }
    return NO;
}

- (void)calcLoadAndSelectMachine:(BOOL)needCalc needChoose:(BOOL)choose andResult:(void (^)(BOOL))result {
    if (needCalc) {
        if ([Tools isEmpty:self.roomTerminalType]) {
            self.roomColdLoad = 0;
            self.roomHeatLoad = 0;
        } else {
            if ([self.roomTerminalType containsString:@"风机盘管"]) {
                self.roomColdLoad = self.roomArea*self.roomColdFactor;
            } else {
                self.roomColdLoad = 0;
            }
            if ([self.roomTerminalType containsString:@"地暖"]) {
                self.roomHeatLoad = self.roomArea*self.roomHeatFactor;
            } else {
                self.roomHeatLoad = 0;
            }
        }
    }
    
    if (!choose) {
        //不需要重新选型
        if (isNexting) {
            [self onNext];
        } else {
            if ([self needReloadTable]) {
                [self.tableView reloadData];
            }
        }
        return;
    }
    
    if (self.roomHeatLoad == self.roomData.heat_load
        && self.roomColdLoad == self.roomData.cold_load
        && [self.roomName isEqualToString:self.roomData.room_name]
        && [self.roomTerminalType isEqualToString:self.roomData.terminal_type]) {
        //不需要重新选型
        if (isNexting) {
            [self onNext];
        } else {
            if ([self needReloadTable]) {
                [self.tableView reloadData];
            }
        }
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.roomData.project_room_id forKey:@"project_room_id"];
    [params setValue:kProject.project_id forKey:@"project_id"];
    [params setValue:self.roomData.room_type_id forKey:@"room_type_id"];
    [params setValue:self.roomData.room_type forKey:@"room_type"];
    [params setValue:self.roomName forKey:@"room_name"];
    [params setValue:self.roomTerminalType forKey:@"terminal_type"];
    [params setValue:@(self.roomArea) forKey:@"area"];
    [params setValue:@(self.roomColdFactor) forKey:@"cold_factor"];
    [params setValue:@(self.roomHeatFactor) forKey:@"heat_factor"];
    [params setValue:@(self.roomColdLoad) forKey:@"cold_load"];
    [params setValue:@(self.roomHeatLoad) forKey:@"heat_load"];
    
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient postWithUrl:HTTP_POST_PROJECT_ROOMS_EDIT token:[AccountManager manager].token paramDic:params resultClass:[RoomData class] success:^(RoomData *obj) {
        //oc对象归档
        [MBProgressHUD hideHUDForView:self.view];
        self.roomData = obj;
        [self copyRoomData];
        if (self.currentIndex < kProject.rooms.count) {
            [kProject.rooms replaceObjectAtIndex:self.currentIndex withObject:obj];
            if (self.roomInfoChangedBlock != nil) {
                self.roomInfoChangedBlock();
            }
            if (self->isNexting) {
                [self onNext];
            } else {
                [self.tableView reloadData];
            }
            result(YES);
        } else {
            result(NO);
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"选型失败！"];
        result(NO);
    }];
}

- (void)updateRoomEquip:(RoomEquipData *)equip andCount:(NSInteger)count {
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:self.roomData.project_room_id forKey:@"project_room_id"];
    [param setObject:kProject.project_id forKey:@"project_id"];
    [param setObject:equip.equip_id forKey:@"equip_id"];
    [param setObject:@(count) forKey:@"count"];
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_ROOMS_FCU_COUNT token:[AccountManager manager].token paramDic:param resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
        //oc对象归档
        [MBProgressHUD hideHUDForView:self.view];
        //        self.roomData.equips = [obj.result mutableCopy];
        //        [kProject.rooms setObject:self.roomData atIndexedSubscript:self.currentIndex];
        //        if (self.roomInfoChangedBlock != nil) {
        //            self.roomInfoChangedBlock();
        //        }
        [self.tableView reloadRowsAtIndexPaths:indexPathObj(6, 0) withRowAnimation:UITableViewRowAnimationAutomatic];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"数据提交失败！"];
    }];
}

- (void)deleteRoomEquip:(NSInteger)index {
    RoomEquipData *equip = [self.roomData.equips objectAtIndex:index];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:self.roomData.project_room_id forKey:@"project_room_id"];
    [param setObject:kProject.project_id forKey:@"project_id"];
    [param setObject:equip.equip_id forKey:@"equip_id"];
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_ROOMS_FCU_DELETE token:[AccountManager manager].token paramDic:param resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
        //oc对象归档
        [MBProgressHUD hideHUDForView:self.view];
        [self.roomData.equips removeObjectAtIndex:index];
//        [self.tableView reloadRowsAtIndexPaths:indexPathObj(7, 0) withRowAnimation:UITableViewRowAnimationAutomatic];
//        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"数据提交失败！"];
    }];
}


- (void)updateRoomEquip:(NSArray *)equips {
    NSMutableArray *ids = [NSMutableArray new];
    if (equips != nil && equips.count > 0) {
        for (NSDictionary *sel in equips) {
            [ids addObject:[sel objectForKey:@"Id"]];
        }
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    if (ids.count > 0) {
        [param setObject:[ids componentsJoinedByString:@","] forKey:@"ids"];
    }
    [param setValue:kProject.project_id forKey:@"project_id"];
    [param setObject:self.roomData.project_room_id forKey:@"project_room_id"];
    
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_ROOMS_FCU_ADD token:[AccountManager manager].token paramDic:param resultClass:[RoomEquipData class] success:^(RoomEquipData *obj) {
        //oc对象归档
        [MBProgressHUD hideHUDForView:self.view];
        self.roomData.equips = [obj.result mutableCopy];
        if (self.roomInfoChangedBlock != nil) {
            self.roomInfoChangedBlock();
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"数据提交失败！"];
    }];
}

- (void)updateRoom:(NSString *)project_room_id PipeCount:(int) count {
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:@(count) forKey:@"pipe_count"];
    [param setObject:self.roomData.project_room_id forKey:@"project_room_id"];
    
    [MBProgressHUD showMessage:@"数据提交中..." toView:self.view];
    [HttpClient getWithUrl:HTTP_GET_PROJECT_ROOMS_PIPE_COUNT token:[AccountManager manager].token paramDic:param resultClass:[RoomEquipData class] success:^(RoomEquipData *obj) {
        //oc对象归档
        [MBProgressHUD hideHUDForView:self.view];
        if (self.roomInfoChangedBlock != nil) {
            self.roomInfoChangedBlock();
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"数据提交失败！"];
    }];
}

#pragma mark - tableView显示设置数据
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger count = 1;
    if (![Tools isEmpty:self.roomTerminalType] &&
        [self.roomTerminalType containsString:@"风机盘管"]) {
        count++;
    }
    if (![Tools isEmpty:self.roomTerminalType] &&
        [self.roomTerminalType containsString:@"地暖"]) {
        count++;
    }
    return count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 8;
    } else if (section == 2) {
        return 5;
    } else {
        if (![Tools isEmpty:self.roomData.terminal_type] &&
            [self.roomTerminalType containsString:@"风机盘管"]) {
            if (self.roomData.equips != nil) {
                return self.roomData.equips.count + 1;
            }
            return 1;
        } else {
            return 5;
        }
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RoomInformationCell];
    cell.accessoryType = UITableViewCellAccessoryNone;
    [cell setEditorText:@""];
    cell.editorView.enabled = YES;
    if (indexPath.section == 0 && indexPath.row == 0) {
        //房间名称
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        [cell setTitleStr:@"房间" andColor:BSBlackTextColor andHintStr:@"请输入房间名称" andInputViewIsHidden:NO andInfoStr:nil andUnit:nil];
        [cell setEditorText:[NSString stringWithFormat:@"%@", self.roomName]];
        [cell setKeyboardType:UIKeyboardTypeDefault];
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            if (value != nil && value.length > 0) {
                self.roomName = [NSString stringWithString:value];
                [self calcLoadAndSelectMachine:NO needChoose:YES andResult:^(BOOL result) {
                    
                }];
            } else {
                [Tools showMessageCenter:@"房间名称不能为空！"];
            }
        };
        
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        //房间面积
        self.areaCell = cell;
        [cell setTitleStr:@"房间面积" andColor:nil andHintStr:@"请输入房间面积" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"㎡"];
        if (self.roomArea > 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%.1f", self.roomArea]];
        }
        [cell setKeyboardType:UIKeyboardTypeDecimalPad];
        cell.TextFiledValueBlock = ^(NSString *value) {
            if ([value doubleValue] > 0) {
                self.roomArea = [value doubleValue];
            }
        };
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            if ([value doubleValue] > 0) {
                self.roomArea = [value doubleValue];
                [self calcLoadAndSelectMachine:YES needChoose:YES andResult:^(BOOL result) {
                    
                }];
            }
        };
    } else if (indexPath.section == 0 && indexPath.row == 2) {
        //末端类型
        TerminalTypeTableViewCell *terCell = [tableView dequeueReusableCellWithIdentifier:@"ter"];
        [terCell setCheckBoxStatus:self.roomTerminalType];
        terCell.checkBoxSelectionChanged = ^(NSString * _Nonnull data) {
            self.roomTerminalType = data;
            [self calcLoadAndSelectMachine:YES needChoose:YES andResult:^(BOOL result) {
                
            }];
        };
        return terCell;
    } else if (indexPath.section == 0 && indexPath.row == 3) {
        //冷负荷指标
        [cell setTitleStr:@"冷负荷指标" andColor:nil andHintStr:@"请输入房间冷负荷指标" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"W/㎡"];
        if (self.roomColdFactor > 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%d", (int)self.roomColdFactor]];
        }
        [cell setKeyboardType:UIKeyboardTypeNumberPad];
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            if ([value doubleValue] > 0) {
                self.roomColdFactor = [value doubleValue];
                [self calcLoadAndSelectMachine:YES needChoose:YES andResult:^(BOOL result) {
                    
                }];
            }
        };
    } else if (indexPath.section == 0 && indexPath.row == 4) {
        //热负荷指标
        [cell setTitleStr:@"热负荷指标" andColor:nil andHintStr:@"请输入房间热负荷指标" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"W/㎡"];
        if (self.roomHeatFactor > 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%d", (int)self.roomHeatFactor]];
        }
        [cell setKeyboardType:UIKeyboardTypeNumberPad];
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            if ([value doubleValue] > 0) {
                self.roomHeatFactor = [value doubleValue];
                [self calcLoadAndSelectMachine:YES needChoose:YES andResult:^(BOOL result) {
                    
                }];
            }
        };
    } else if (indexPath.section == 0 && indexPath.row == 5) {
        //房间负荷
        [cell setTitleStr:@"房间冷负荷" andColor:nil andHintStr:@"请输入房间冷负荷" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"W"];
        if (self.roomColdLoad > 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%d", (int)self.roomColdLoad]];
        }
        [cell setKeyboardType:UIKeyboardTypeNumberPad];
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            if ([value doubleValue] > 0) {
                self.roomColdLoad = [value doubleValue];
                [self calcLoadAndSelectMachine:NO needChoose:YES andResult:^(BOOL result) {
                    
                }];
            }
        };
    } else if (indexPath.section == 0 && indexPath.row == 7) {
        //房间负荷
        [cell setTitleStr:@"房间热负荷" andColor:nil andHintStr:@"请输入房间热负荷" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"W"];
        if (self.roomHeatLoad > 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%d", (int)self.roomHeatLoad]];
        }
        [cell setKeyboardType:UIKeyboardTypeNumberPad];
        cell.TextFiledEditingEndBlock = ^(NSString *value){
            if ([value doubleValue] > 0) {
                self.roomHeatLoad = [value doubleValue];
                [self calcLoadAndSelectMachine:NO needChoose:YES andResult:^(BOOL result) {
                    
                }];
            }
        };
    } else if (indexPath.section == 0 && indexPath.row == 6) {
        //房间负荷
        [cell setTitleStr:@"风盘制冷能力" andColor:nil andHintStr:@"" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"W"];
        double load = 0;
        if (self.roomData.equips != nil && self.roomData.equips.count > 0) {
            for (RoomEquipData *equip in self.roomData.equips) {
                load += equip.equip_load*1000.0*equip.count;
            }
        }
        [cell setEditorText:[NSString stringWithFormat:@"%d", (int)load]];
        cell.editorView.enabled = NO;
    } else if (indexPath.section == 1) {
        if (![Tools isEmpty:self.roomTerminalType] &&
            [self.roomTerminalType containsString:@"风机盘管"]) {
            //室内机
            if (indexPath.row == 0) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                [cell setTitleStr:@"室内机" andColor:BSBlackTextColor andHintStr:nil andInputViewIsHidden:YES andInfoStr:nil andUnit:nil];
                [cell setNavigationHint:@"手动选择"];
            }
            else {
                RoomEquipData *data = [self.roomData.equips objectAtIndex:indexPath.row - 1];
                
                static NSString *identifier = @"snjMachineSelectCell";
                MachineSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
                if (cell == nil) {
                    cell = [[MachineSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
                }
                [cell setNumber:data.count andUnit:@"台"];
                [cell setTitle:data.equip_model];
                cell.SteperValueChangedBlock = ^(int number){
                    if (number > 0) {
                        [self updateRoomEquip:data andCount:number];
                        data.count = number;
                    } else {
                        [self deleteRoomEquip:indexPath.row - 1];
                    }
                };
                
                //滑动按钮
                cell.rightUtilityButtons = [self rightButtons];
                cell.delegate = self;
                cell.tag = indexPath.row - 1;
                return cell;
            }
        } else {
            //地热盘管
            cell.editorView.enabled = NO;
            if (indexPath.row == 0) {
                [cell setTitleStr:@"地热盘管" andColor:BSBlackTextColor andHintStr:nil andInputViewIsHidden:YES andInfoStr:nil andUnit:nil];
                [cell setNavigationHint:@""];
            } else if (indexPath.row == 1){
                [cell setTitleStr:@"管径" andColor:nil andHintStr:@"请输入管径" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"mm"];
                [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_diameter]];
                [cell setKeyboardType:UIKeyboardTypeNumberPad];
                cell.editorView.enabled = NO;
                cell.TextFiledEditingEndBlock = ^(NSString *value){
                    if ([value intValue] > 0) {
                        self.roomData.pipe_diameter = [value intValue];
                    }
                };
            } else if (indexPath.row == 2){
                [cell setTitleStr:@"盘管间距" andColor:nil andHintStr:@"请输入盘管间距" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"mm"];
                [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_space]];
                [cell setKeyboardType:UIKeyboardTypeNumberPad];
                cell.editorView.enabled = NO;
                cell.TextFiledEditingEndBlock = ^(NSString *value){
                    if ([value intValue] > 0) {
                        self.roomData.pipe_space = [value intValue];
                    }
                };
            } else if (indexPath.row == 3){
                [cell setTitleStr:@"近似长度" andColor:nil andHintStr:@"请输入近似长度" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"m"];
                [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_length]];
                [cell setKeyboardType:UIKeyboardTypeNumberPad];
                cell.editorView.enabled = NO;
                cell.TextFiledEditingEndBlock = ^(NSString *value){
                    if ([value intValue] > 0) {
                        self.roomData.pipe_length = [value intValue];
                    }
                };
            } else {
                cell.editorView.enabled = YES;
                [cell setTitleStr:@"盘管路数" andColor:nil andHintStr:@"请输入盘管路数" andInputViewIsHidden:NO andInfoStr:nil andUnit:nil];
                [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_count]];
                [cell setKeyboardType:UIKeyboardTypeNumberPad];
                cell.TextFiledEditingEndBlock = ^(NSString *value){
                    if ([value intValue] >= 0) {
                        self.roomData.pipe_count = [value intValue];
                        //提交数据
                        [self updateRoom:self.roomData.project_room_id PipeCount:[value intValue]];
                    }
                };
            }
            return cell;
        }
        
    }
    else {
        //地热盘管
        cell.editorView.enabled = NO;
        if (indexPath.row == 0) {
            [cell setTitleStr:@"地热盘管" andColor:nil andHintStr:nil andInputViewIsHidden:YES andInfoStr:nil andUnit:nil];
            [cell setNavigationHint:@""];
        } else if (indexPath.row == 1){
            [cell setTitleStr:@"管径" andColor:nil andHintStr:@"请输入管径" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"mm"];
            [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_diameter]];
            [cell setKeyboardType:UIKeyboardTypeNumberPad];
            cell.TextFiledEditingEndBlock = ^(NSString *value){
                if ([value intValue] > 0) {
                    self.roomData.pipe_diameter = [value intValue];
                }
            };
        } else if (indexPath.row == 2){
            [cell setTitleStr:@"盘管间距" andColor:nil andHintStr:@"请输入盘管间距" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"mm"];
            [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_space]];
            [cell setKeyboardType:UIKeyboardTypeNumberPad];
            cell.TextFiledEditingEndBlock = ^(NSString *value){
                if ([value intValue] > 0) {
                    self.roomData.pipe_space = [value intValue];
                }
            };
        } else if (indexPath.row == 3){
            [cell setTitleStr:@"近似长度" andColor:nil andHintStr:@"请输入近似长度" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"m"];
            [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_length]];
            [cell setKeyboardType:UIKeyboardTypeNumberPad];
            cell.TextFiledEditingEndBlock = ^(NSString *value){
                if ([value intValue] > 0) {
                    self.roomData.pipe_length = [value intValue];
                }
            };
        } else {
            cell.editorView.enabled = YES;
            [cell setTitleStr:@"盘管路数" andColor:nil andHintStr:@"请输入盘管路数" andInputViewIsHidden:NO andInfoStr:nil andUnit:nil];
            [cell setEditorText:[NSString stringWithFormat:@"%d", self.roomData.pipe_count]];
            [cell setKeyboardType:UIKeyboardTypeNumberPad];
            cell.TextFiledEditingEndBlock = ^(NSString *value){
                if ([value intValue] >= 0) {
                    self.roomData.pipe_count = [value intValue];
                    //提交数据
                    [self updateRoom:self.roomData.project_room_id PipeCount:[value intValue]];
                }
            };
        }
        return cell;
    }
    return cell;
}

-(NSArray *)rightButtons {
    NSMutableArray *rightUtilityButtons = [NSMutableArray new];
    [rightUtilityButtons sw_addUtilityButtonWithColor:[UIColor redColor] title:@"删除"];
    return rightUtilityButtons;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    if (indexPath.section == 1 && indexPath.row == 0) {
        //室内机
        FjpgViewController *vc = [FjpgViewController new];
        if (self.roomData.equips.count > 0) {
            vc.selectedGuids = [NSMutableArray new];
            for (RoomEquipData *data in self.roomData.equips) {
                [vc.selectedGuids addObject:data.equip_id];
            }
        }
        vc.machineSelectedBlock = ^(NSArray *equips) {
            //            self.roomData.equips = [NSMutableArray new];
            //            if (equips != nil && equips.count > 0) {
            //                for (NSDictionary *sel in equips) {
            //                    RoomEquipData *equip = [RoomEquipData new];
            //                    [equip setEquip_id:[sel objectForKey:@"Id"]];
            //                    [equip setEquip_model:[sel objectForKey:@"Model"]];
            //                    [equip setCount:1];
            //                    [self.roomData.equips addObject:equip];
            //                }
            //            }
            [self updateRoomEquip:equips];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (![Tools isEmpty:self.roomTerminalType] &&
            [self.roomTerminalType containsString:@"风机盘管"] &&
            indexPath.row > 0) {
            static NSString *identifier = @"snjMachineSelectCell";
            MachineSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[MachineSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
            }
            return [cell getHeigh];
        }
    }
    static NSString *identifier = @"RoomInformationCell";
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return [cell getHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, DEFAULT_SECTION_HEIGHT)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return DEFAULT_SECTION_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


#pragma mark - 滑动按钮代理协议
/**
 *  右侧滑动按钮点击事件
 *
 *  @param cell  单元cell
 *  @param index 下标
 */
-(void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index{
    switch (index) {
        case 0:{
            [self deleteRoomEquip:cell.tag];
            break;
        }
        default:
            break;
    }
    [cell hideUtilityButtonsAnimated:YES];
}


/**
 *  滑动的时候把其他的隐藏掉
 *
 *  @param cell cell
 *
 *  @return 隐藏其他的cell
 */
-(BOOL)swipeableTableViewCellShouldHideUtilityButtonsOnSwipe:(SWTableViewCell *)cell{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
